# -*- coding: utf-8 -*-
import os
import datetime


def get_log_file_path(base_dir='logs'):
    """
    获取当天的日志文件路径，如果目录不存在则创建。
    """
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

    today = datetime.date.today()
    log_file_name = today.strftime('%Y-%m-%d') + '.log'
    log_file_path = os.path.join(base_dir, log_file_name)
    return log_file_path


def write_log(message, log_level='INFO', base_dir='logs'):
    """
    写入日志到当天的日志文件中，自动添加写入时间和日志级别。
    """
    log_file_path = get_log_file_path(base_dir)

    # 获取当前时间
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y-%m-%d %H:%M:%S')

    # 格式化日志内容
    log_entry = f"{timestamp} [{log_level}] {message}\n"

    # 写入日志文件
    with open(log_file_path, 'a', encoding='utf8') as log_file:
        log_file.write(log_entry)



# if __name__ == '__main__':
#     # 示例调用
#     write_log("This is an info message.")
#     write_log("哈哈哈.", log_level='WARNING')
#     write_log("This is an error message.", log_level='ERROR')
